using System;
using System.Runtime.InteropServices;

namespace daq_test_cs
{
    /// <summary>
    /// DAQ库日志管理器的C#封装类
    /// 提供日志等级设置和查询功能
    /// </summary>
    public class DAQLoggerWrapper
    {
        /// <summary>
        /// 日志等级枚举
        /// 定义了DAQ库支持的所有日志等级
        /// </summary>
        public enum LogLevel : int
        {
            /// <summary>
            /// 最详细的调试信息
            /// </summary>
            Trace = 0,
            
            /// <summary>
            /// 调试信息
            /// </summary>
            Debug = 1,
            
            /// <summary>
            /// 一般信息
            /// </summary>
            Info = 2,
            
            /// <summary>
            /// 警告信息
            /// </summary>
            Warn = 3,
            
            /// <summary>
            /// 错误信息
            /// </summary>
            Error = 4,
            
            /// <summary>
            /// 严重错误信息
            /// </summary>
            Critical = 5,
            
            /// <summary>
            /// 关闭所有日志输出
            /// </summary>
            Off = 6
        }

        private const string DllPath = "./libdaq-3.10.1.dll";

        /// <summary>
        /// 设置日志输出等级
        /// </summary>
        /// <param name="level">日志等级，使用LogLevel枚举值</param>
        /// <remarks>
        /// 设置日志等级后，只有等级大于或等于设定等级的日志才会被输出。
        /// 例如，设置为Warn等级时，只有Warn、Error和Critical级别的日志会被输出。
        /// </remarks>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern void DAQLibrary_SetLogLevel(int level);

        /// <summary>
        /// 获取当前日志输出等级
        /// </summary>
        /// <returns>当前日志等级，返回LogLevel枚举值之一</returns>
        /// <remarks>
        /// 该函数用于查询当前设置的日志等级，可用于在用户界面中
        /// 显示当前日志配置或进行条件性日志处理。
        /// </remarks>
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern int DAQLibrary_GetLogLevel();

        /// <summary>
        /// 设置日志等级（使用枚举类型）
        /// </summary>
        /// <param name="level">日志等级枚举值</param>
        public static void SetLogLevel(LogLevel level)
        {
            DAQLibrary_SetLogLevel((int)level);
        }

        /// <summary>
        /// 获取当前日志等级（返回枚举类型）
        /// </summary>
        /// <returns>当前日志等级枚举值</returns>
        public static LogLevel GetLogLevel()
        {
            int level = DAQLibrary_GetLogLevel();
            return (LogLevel)level;
        }

        /// <summary>
        /// 检查指定日志等级是否会被输出
        /// </summary>
        /// <param name="level">要检查的日志等级</param>
        /// <returns>如果该等级的日志会被输出则返回true，否则返回false</returns>
        public static bool IsLogLevelEnabled(LogLevel level)
        {
            LogLevel currentLevel = GetLogLevel();
            return level >= currentLevel && currentLevel != LogLevel.Off;
        }

        /// <summary>
        /// 获取日志等级的字符串描述
        /// </summary>
        /// <param name="level">日志等级</param>
        /// <returns>日志等级的字符串描述</returns>
        public static string GetLogLevelDescription(LogLevel level)
        {
            switch (level)
            {
                case LogLevel.Trace:
                    return "最详细的调试信息";
                case LogLevel.Debug:
                    return "调试信息";
                case LogLevel.Info:
                    return "一般信息";
                case LogLevel.Warn:
                    return "警告信息";
                case LogLevel.Error:
                    return "错误信息";
                case LogLevel.Critical:
                    return "严重错误信息";
                case LogLevel.Off:
                    return "关闭所有日志输出";
                default:
                    return "未知日志等级";
            }
        }
    }
}